package com.yaozheng.vocationaltraining.adapter;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ImageUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.MaintenanceEncyclopediaItemView;
import com.yaozheng.vocationaltraining.view.MaintenanceEncyclopediaItemView_;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MaintenanceEncyclopediaAdapter extends BaseAdapter {
	BaseActivity baseActivity;
	JSONArray dataList;
	// DisplayImageOptions displayImageOptions;
	BitmapLoader bitmapLoader;

	public MaintenanceEncyclopediaAdapter(BaseActivity baseActivity,
			JSONArray dataList) {
		super();
		this.baseActivity = baseActivity;
		this.dataList = dataList;
		// displayImageOptions = ImageUtils
		// .getDisplayImageOptions(R.drawable.default_news_image);
		bitmapLoader = new BitmapLoader(Glide.with(baseActivity),
				R.drawable.default_news_image);
	}

	public JSONArray getDataList() {
		return dataList;
	}

	/**
	 * 得到最后id
	 * 
	 * @return
	 */
	public int getLastId() {
		if (dataList != null && TypeUtils.getJsonArraySize(dataList) > 0) {
			JSONObject itemData = TypeUtils.getJsonObject(dataList,
					TypeUtils.getJsonArraySize(dataList) - 1);
			if (itemData != null) {
				return TypeUtils.getJsonInteger(itemData, "id", -1);
			}
		}
		return -1;
	}

	@Override
	public int getCount() {
		return TypeUtils.getJsonArraySize(dataList);
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MaintenanceEncyclopediaItemView maintenanceEncyclopediaItemView;
		if (convertView == null) {
			maintenanceEncyclopediaItemView = MaintenanceEncyclopediaItemView_
					.build(baseActivity);
		} else {
			maintenanceEncyclopediaItemView = (MaintenanceEncyclopediaItemView) convertView;
		}
		maintenanceEncyclopediaItemView.loadData(
				TypeUtils.getJsonObject(dataList, position), position,
				bitmapLoader);
		return maintenanceEncyclopediaItemView;
	}
}
